package be.vdab.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

@Repository("namenDAOXML")
@Qualifier("XMLFormaat")
class NamenDAOXML implements NamenDAO {
	private File bestand;
	@Autowired
	public NamenDAOXML(@Value("${namenXML}")File bestand) {
	  this.bestand = bestand;
	  System.out.println("constructor " + this.getClass().getName());
	} 
	@Override
	public void opslaan(Collection<String> namen){
		try(PrintStream stream = new PrintStream(bestand, "UTF-8")){
			stream.println("<namen>");
			for(String naam : namen){
				stream.println("\t<naam>" + naam + "</naam>");
			}
			stream.println("</namen");
		}catch(IOException ex){
			throw new NamenDAOException(ex);
		}
	}
	@Override
	public int getAantalOpgeslagenNamen(){
		if(!bestand.exists()){
			return 0;
		}
		try(BufferedReader reader = new BufferedReader(new FileReader(bestand))){
			int aantalNamen = 0;
			for(String regel;(regel = reader.readLine()) != null;){
				if(regel.contains("<naam>")){
					aantalNamen++;
				}
			}
			return aantalNamen;
		}catch(IOException ex){
			throw new NamenDAOException(ex);
		}
	}
}
